import { ADD_TODO, DELETE_TODO } from "../constants/ActionTypes";


const initialState = [
    {id:0, text:'第一个代表事项(系统默认生成)', completed: false}
];

export default function todos(state = initialState, action = {}){
    switch  (action.type)
    {
        case ADD_TODO:
            return [
                ...state,
                {
                    id: state.reduce((maxId, todo) => Math.max(maxId, todo.id), -1 ) + 1,
                    text: action.text,
                    completed: false
                }
            ]
        case DELETE_TODO:
            return state.filter(todo => todo.id !== action.id )
        default:
            return state
    }
}